/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.dictionary.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.dictionary.DictionaryDao;
import org.grayrabbit.cms.dao.dictionary.DictionaryTypeDao;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.entity.DictionaryType;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.dictionary.DictionaryTypeService;
import org.grayrabbit.cms.vo.dictionary.DictionaryBean;
import org.grayrabbit.cms.vo.dictionary.DictionaryTypeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 14:46:53  
 */
@Service
@Transactional
public class DictionaryTypeServiceImpl extends GenericServiceImpl implements DictionaryTypeService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("dictionaryTypeDaoImpl")
	private DictionaryTypeDao dictionaryTypeDao;
	
	@Autowired
	@Qualifier("dictionaryDaoImpl")
	private DictionaryDao dictionaryDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see DictionaryTypeService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public DictionaryType getDictionaryType(final String dicTypeId){
		DictionaryType dictionaryType = dictionaryTypeDao.findById(dicTypeId);
		dictionaryTypeDao.initialize(dictionaryType);
		return dictionaryType;
	}
	
	/**
	 * @see DictionaryTypeService#delete(String)
	 */
	@Transactional
	public void delete(final DictionaryType dictionaryType) {
		synchronized (DictionaryContext.class) {
			dictionaryTypeDao.makeTransient(dictionaryType);
			DictionaryContext.UPDATE_FLAG = true;
		}
	}
	
	/**
	 * @see DictionaryTypeService#save(OperLog)
	 */
	@Transactional
	public void save(final DictionaryType dictionaryType) {
		synchronized (DictionaryContext.class) {
			dictionaryTypeDao.makePersistent(dictionaryType);
			DictionaryContext.UPDATE_FLAG = true;
		}
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see DictionaryTypeService#getPage(DictionaryTypeBean)
	 */
	@Transactional(readOnly=true)
	public Page<DictionaryType> getPage(final DictionaryTypeBean dictionaryTypeBean){
		Page<DictionaryType> page =  new Page<DictionaryType>();
		page.setTotal(dictionaryTypeDao.count(dictionaryTypeBean));
		page.setResult(dictionaryTypeDao.findByQc(dictionaryTypeDao.assembly(dictionaryTypeBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see DictionaryTypeService#list(Page, DictionaryTypeBean)
	 */
	public void list(Page<DictionaryType> page, DictionaryTypeBean dictionaryTypeBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(dictionaryTypeDao.count(dictionaryTypeBean));
		}
		page.setResult(dictionaryTypeDao.findByPage(page,dictionaryTypeBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see DictionaryTypeService#delete(String[])
	 */
	public void delete(final String[] key) {
		synchronized (DictionaryContext.class) {
			int i =0;
			for(String id : key){
				DictionaryBean bean = new DictionaryBean();
				bean.setDictionaryType(dictionaryTypeDao.findById(id));
				List<Dictionary> list = dictionaryDao.findByDicTypeId(bean);
				if(!list.isEmpty()){
					for(Dictionary item:list){
						dictionaryDao.makeTransient(item);
					}
				}
				dictionaryTypeDao.makeTransient(dictionaryTypeDao.findById(id));
				i++;
				if(i==MAX_CLEAR)
					dictionaryTypeDao.clear();
			}
			DictionaryContext.UPDATE_FLAG = true;
		}
	}

	public DictionaryType ftlFindDicTypeByTypeKey(String key) {
		if(StringUtils.isBlank(key)) key = DictionaryStaticValues.TAX_TYPE;
		DictionaryType type = dictionaryTypeDao.findByKey(key); 
		if(type != null){
			List<Dictionary> list = dictionaryDao.findTopLevelDicByTypeId(type.getDicTypeId());
			for(Dictionary d : list){
				dictionaryDao.initialize(d.getDictionaries());
			}
			type.setDics(list);
			
		}
		return type;
	}
 
}